<?php
	include("global_modules.php");
	error_reporting(0);
	
	/* API Controller */
	$testbed_methods	=	array(
								'get_testbed_info'
							);
	$users_methods		=	array(
								'authenticate_user',
								'get_users',
								'add_user',
								'add_user_public_key',
								'delete_user',
								'delete_user_public_key',
								'update_user',
								'get_user_id_by_username'
							);
	$resources_methods	=	array(
								'get_resources',
								'get_slices',
								'get_pxeimages',
								'get_motherboards',
								'add_resource',
								'add_slice',
								'add_user_to_slice',
								'add_resource_to_slice',
								'add_pxeimage',
								'add_motherboard',
								'delete_resource',
								'delete_slice',
								'delete_user_from_slice',
								'delete_resource_from_slice',
								'delete_pxeimage',
								'delete_motherboard',
								'update_resource',
								'update_slice',
								'update_pxeimage',
								'update_motherboard'
							);
	$reserves_methods	=	array(
								'get_my_reservations',
                                'get_reserved_resources',
								'get_free_resources',
								'is_resource_available',
								'create_reservation',
								'cancel_reservation',
								'delete_resource_from_reservation'
							);
							
	$ldap_methods		=	array(
								'get_ldap_pendencies',
								'resolve_ldap_pendecy'
							);
	
	$api_call = $_GET['method'];
	$call_status = 0;
	
	/* Initial verifications */
	if(!authentication())
		$call_status = 1;
	
	if(in_array($api_call, $testbed_methods))
		require("testbed.php");
	else if(in_array($api_call, $users_methods))
		require("users.php");
	else if(in_array($api_call, $resources_methods))
		require("resources.php");
	else if(in_array($api_call, $reserves_methods))
		require("reserves.php");
	else if(in_array($api_call, $ldap_methods))
		require("ldap.php");
	else
		$call_status = 3;
	
	/**
	 * If have no problem in the initial verifications, proceed
	 * Get the parameters for the method.
	 */
	if($call_status == 0){
		/* Default parameter is a null array */
		$obligatory_parameters = array(null);
		$optional_parameters = array(null);
		
		switch($api_call){
			case "authenticate_user":
				$obligatory_parameters = array('username' => 'string', 'password' => 'string');
			break;
			case "get_user_id_by_username":
				$obligatory_parameters = array('username' => 'string');
			break;
			case "get_resources":
				$optional_parameters = array('filter' => 'array', 'type' => 'string');
			break;
			case "get_users":
			case "get_slices":
			case "get_pxeimages":
			case "get_motherboards":
				$optional_parameters = array('filter' => 'array');
			break;
			case "add_user":
				$obligatory_parameters = array('username' => 'string', 'email' => 'string');
				$optional_parameters = array('password' => 'string', 'name' => 'string', 'gidnumber' => 'numeric', 'homedirectory' => 'string');
			break;
			case "add_user_public_key":
				$obligatory_parameters = array('user_id' => 'numeric', 'public_key' => 'string');
			break;
			case "delete_user":
			case "get_my_reservations":
			case "delete_user_public_key":
				$obligatory_parameters = array('user_id' => 'numeric');
			break;
            case "get_reserved_resources":
                $optional_parameters = array('type' => 'string');
            break;
			case "update_user":
				$obligatory_parameters = array('user_id' => 'numeric', 'new_user_data' => 'array');
			break;
			case "add_resource":
				$obligatory_parameters = array('type' => 'string', 'resource_data' => 'array');
			break;
			case "add_slice":
				$obligatory_parameters = array('slice_data' => 'array');
				$optional_parameters = array('expire_date' => 'string');
			break;
			case "add_user_to_slice":
			case "delete_user_from_slice":
				$obligatory_parameters = array('user_id' => 'numeric', 'slice_id' => 'numeric');
			break;
			case "add_resource_to_slice":
			case "delete_resource_from_slice":
				$obligatory_parameters = array('resource_id' => 'numeric', 'slice_id' => 'numeric');
			break;
			case "add_pxeimage":
				$obligatory_parameters = array('pxeimage_data' => 'array');
			break;
			case "add_motherboard":
				$obligatory_parameters = array('motherboard_data' => 'array');
			break;
			case "delete_resource":
				$obligatory_parameters = array('resource_id' => 'numeric');
			break;
			case "delete_slice":
				$obligatory_parameters = array('slice_id' => 'numeric');
			break;
			case "delete_pxeimage":
				$obligatory_parameters = array('pxeimage_id' => 'numeric');
			break;
			case "delete_motherboard":
				$obligatory_parameters = array('motherboard_id' => 'numeric');
			break;
			case "update_resource":
				$obligatory_parameters = array('resource_id' => 'numeric', 'resource_new_data' => 'array');
			break;
			case "update_slice":
				$obligatory_parameters = array('slice_id' => 'numeric');
                $optional_parameters = array('name' => 'string', 'expires' => 'string');
			break;
			case "update_pxeimage":
				$obligatory_parameters = array('pxeimage_id' => 'numeric', 'pxeimage_new_data' => 'array');
			break;
			case "update_motherboard":
				$obligatory_parameters = array('motherboard_id' => 'numeric', 'motherboard_new_data' => 'array');
			break;
			case "get_free_resources":
				$obligatory_parameters = array('begin_time' => 'string', 'end_time' => 'string');
				$optional_parameters = array('type' => 'string');
			break;
			case "is_resource_available":
				$obligatory_parameters = array('resource_id' => 'numeric', 'begin_time' => 'string', 'end_time' => 'string');
			break;
			case "create_reservation":
				$obligatory_parameters = array('user_id' => 'numeric', 'slice_id' => 'numeric', 'begin_time' => 'string', 'end_time' => 'string');
			break;
			case "cancel_reservation":
				$obligatory_parameters = array('reservation_id' => 'numeric');
			break;
			case "delete_resource_from_reservation":
				$obligatory_parameters = array('reservation_id' => 'numeric', 'resource_id' => 'numeric');
			break;
			case "resolve_ldap_pendecy":
				$obligatory_parameters = array('pendency_id' => 'numeric');
			break;
		}
		
		$received_parameters = verify_paramenters($_GET, $obligatory_parameters, $optional_parameters);
		if(!$received_parameters)
			$call_status = 2;
	}
	
	/**
	 * If have no problem in the initial verifications, proceed
	 * Calling the real methods
	 */
	if($call_status == 0){
		switch($api_call){
			case "get_testbed_info":
				$return['method_result'] = get_testbed_info();
			break;
			case "authenticate_user":
				$return['method_result'] = authenticate_user($received_parameters['username'], $received_parameters['password']);
			break;
			case "get_user_id_by_username":
				$return['method_result'] = get_user_id_by_username($received_parameters['username']);
			break;
			case "get_users":
				$return['method_result'] = get_users($received_parameters['filter']);
			break;
			case "add_user":
				$return['method_result'] = add_user($received_parameters);
			break;
			case "add_user_public_key":
				$return['method_result'] = add_user_public_key($received_parameters['user_id'], $received_parameters['public_key']);
			break;
			case "delete_user":
				$return['method_result'] = delete_user($received_parameters['user_id']);
			break;
			case "delete_user_public_key":
				$return['method_result'] = delete_user_public_key($received_parameters['user_id']);
			break;
			case "update_user":
				$return['method_result'] = update_user($received_parameters['user_id'], $received_parameters['new_user_data']);
			break;
			case "get_resources":
				$return['method_result'] = get_resources($received_parameters['filter'], $received_parameters['type']);
			break;
			case "get_slices":
				$return['method_result'] = get_slices($received_parameters['filter']);
			break;
			case "get_pxeimages":
				$return['method_result'] = get_pxeimages($received_parameters['filter']);
			break;
			case "get_motherboards":
				$return['method_result'] = get_motherboards($received_parameters['filter']);
			break;
			case "add_resource":
				$return['method_result'] = add_resource($received_parameters['type'], $received_parameters['resource_data']);
			break;
			case "add_slice":
				$return['method_result'] = add_slice($received_parameters['slice_data'], $received_parameters['expire_date']);
			break;
			case "add_user_to_slice":
				$return['method_result'] = add_user_to_slice($received_parameters['user_id'], $received_parameters['slice_id']);
			break;
			case "add_resource_to_slice":
				$return['method_result'] = add_resource_to_slice($received_parameters['resource_id'], $received_parameters['slice_id']);
			break;
			case "add_pxeimage":
				$return['method_result'] = add_pxeimage($received_parameters['pxeimage_data']);
			break;
			case "add_motherboard":
				$return['method_result'] = add_motherboard($received_parameters['motherboard_data']);
			break;
			case "delete_resource":
				$return['method_result'] = delete_resource($received_parameters['resource_id']);
			break;
			case "delete_slice":
				$return['method_result'] = delete_slice($received_parameters['slice_id']);
			break;
			case "delete_user_from_slice":
				$return['method_result'] = delete_user_from_slice($received_parameters['user_id'], $received_parameters['slice_id']);
			break;
			case "delete_resource_from_slice":
				$return['method_result'] = delete_resource_from_slice($received_parameters['resource_id'], $received_parameters['slice_id']);
			break;
			case "delete_pxeimage":
				$return['method_result'] = delete_pxeimage($received_parameters['pxeimage_id']);
			break;
			case "delete_motherboard":
				$return['method_result'] = delete_motherboard($received_parameters['motherboard_id']);
			break;
			case "update_resource":
				$return['method_result'] = update_resource($received_parameters['resource_id'], $received_parameters['resource_new_data']);
			break;
			case "update_slice":
				$return['method_result'] = update_slice($received_parameters['slice_id'], $received_parameters['name'], $received_parameters['expires']);
			break;
			case "update_pxeimage":
				$return['method_result'] = update_pxeimage($received_parameters['pxeimage_id'], $received_parameters['pxeimage_new_data']);
			break;
			case "update_motherboard":
				$return['method_result'] = update_motherboard($received_parameters['motherboard_id'], $received_parameters['motherboard_new_data']);
			break;
			case "get_my_reservations":
				$return['method_result'] = get_my_reservations($received_parameters['user_id']);
			break;
            case "get_reserved_resources":
                $return['method_result'] = get_reserved_resources($received_parameters['type']);
            break;
			case "get_free_resources":
				$return['method_result'] = get_free_resources($received_parameters['begin_time'], $received_parameters['end_time'], $received_parameters['type']);
			break;
			case "is_resource_available":
				$return['method_result'] = is_resource_available($received_parameters['resource_id'], $received_parameters['begin_time'], $received_parameters['end_time']);
			break;
			case "create_reservation":
				$return['method_result'] = create_reservation($received_parameters['user_id'], $received_parameters['slice_id'], $received_parameters['begin_time'], $received_parameters['end_time']);
			break;
			case "cancel_reservation":
				$return['method_result'] = cancel_reservation($received_parameters['reservation_id']);
			break;
			case "delete_resource_from_reservation":
				$return['method_result'] = delete_resource_from_reservation($received_parameters['resource_id'], $received_parameters['reservation_id']);
			break;
			case "get_ldap_pendencies":
				$return['method_result'] = get_ldap_pendencies();
			break;
			case "resolve_ldap_pendecy":
				$return['method_result'] = resolve_ldap_pendecy($received_parameters['pendency_id']);
			break;
		}
	}
	
	/* Adding the call status into the return */
	$return['call_status'] = $call_status;
	
	/* Sending the data in JSON */
	exit(json_encode($return));
?>
